package cn.tedu.csmall.product.controller;


import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;

import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;
import java.util.List;


/**
 * 处理品牌相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/brands")
@Api(tags = "1.品牌管理模块")
public class BrandController {

    @Autowired
    private IBrandService brandService;

    @ApiOperation("添加品牌")
    @PostMapping("/add-new")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid BrandAddNewDTO brandAddNewDTO){
        System.out.println("即将处理【添加品牌】请求......");
        System.out.println("brandAddNewDTO = " + brandAddNewDTO);

            brandService.addNew(brandAddNewDTO);
            return JsonResult.ok();
    }

    //http://localhost:9080/brands/3/deleteById
    @ApiOperation("根据id删除品牌")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType = "long")
    @PostMapping("/{id:[0-9]+}/deleteById")
    public JsonResult<Void> deleteById(@PathVariable  Long id){
        log.debug("即将处理【删除品牌】的请求,id={}",id);
            brandService.deleteById(id);
            return JsonResult.ok();
    }

    @Deprecated
    @ApiOperation("测试：删除品牌")
    @ApiOperationSupport(order = 901)
    @PostMapping("/test/delete")
    @ApiImplicitParam(name="enable",dataType = "int",paramType = "query")
    public String delete(@Range(max = 1,message = "测试失败，enable参数必须是0-1之间的数") Integer enable){
        System.out.println("即将处理【测试删除品牌】的请求......");
        log.debug("enable = {}", enable);
        throw new RuntimeException("此接口仅限于测试！");
    }
    //http://localhost:9080/brands/3/update
    @PostMapping("/{id:[1-9]+}/update")
    @ApiOperation("修改品牌信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType = "long")
    public JsonResult<Void> update(@PathVariable Long id, BrandUpdateDTO brandUpdateDTO){
        log.debug("开始处理【修改品牌详情】的请求：id={},brandUpdateDTO={}",id,brandUpdateDTO);
        brandService.updateById(id,brandUpdateDTO);
        return JsonResult.ok();
    }

    //http://localhost:9080/brands/3/setEnable
    @ApiOperation("启用品牌")
    @PostMapping("/{id:[0-9]+}/setEnable")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType = "long")
    public JsonResult<Void> setEnable(@PathVariable Long id){
        log.debug("即将处理【启用品牌】的请求,id={}",id);

            brandService.setEnable(id);
            return JsonResult.ok();
    }

    //http://localhost:9080/brands/3/setUnEnable
    @ApiOperation("禁用品牌")
    @PostMapping("/{id:[0-9]+}/setUnEnable")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType = "long")
    public JsonResult<Void> setUnEnable(@PathVariable Long id){
        log.debug("即将处理【禁用品牌】的请求,id={}",id);
            brandService.setUnEnable(id);
            return JsonResult.ok();
    }

    @ApiOperation("根据id获取品牌表的详细信息")
    @GetMapping("/{id:[0-9]+}")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id",value = "类别id",required = true,dataType = "long")
    public JsonResult<BrandStandardVO> getBrandStandardVO(@PathVariable Long id){
        log.debug("即将处理【根据id获取类别表的详细信息】请求");
        BrandStandardVO brand = brandService.getStandardById(id);
        return JsonResult.ok(brand);
    }

    @ApiOperation("品牌列表")
    @ApiOperationSupport(order = 410)
    @GetMapping("")
    public JsonResult<List<BrandListItemVO>> list(){
        log.debug("开始处理【品牌列表请求】");
        List<BrandListItemVO> list = brandService.list();
        return JsonResult.ok(list);
    }
}
